#include <gtk/gtk.h>
#include <iostream>

// 方法1: 使用gtk_widget_get_name获取按钮名称
void on_button_clicked_get_name(GtkWidget *widget, gpointer data) {
    const gchar *button_name = gtk_widget_get_name(widget);
    std::cout << "按钮名称: " << button_name << std::endl;
}

// 方法2: 使用g_object_get_data获取按钮关联的数据
void on_button_clicked_get_data(GtkWidget *widget, gpointer data) {
    gpointer button_id = g_object_get_data(G_OBJECT(widget), "button-id");
    if (button_id) {
        std::cout << "按钮ID: " << (const char *)button_id << std::endl;
    } else {
        std::cout << "未找到按钮ID" << std::endl;
    }
}

// 方法3: 在回调函数中直接使用按钮标签作为标识符
void on_button_clicked_use_label(GtkWidget *widget, gpointer data) {
    const gchar *button_label = gtk_button_get_label(GTK_BUTTON(widget));
    std::cout << "按钮标签: " << button_label << std::endl;
}

int main(int argc, char *argv[]) {
    gtk_init(&argc, &argv);

    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "按钮ID示例");
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_container_add(GTK_CONTAINER(window), box);

    // 创建按钮并设置名称
    GtkWidget *button1 = gtk_button_new_with_label("按钮1");
    gtk_widget_set_name(button1, "button_one");  // 设置按钮名称
    g_signal_connect(button1, "clicked", G_CALLBACK(on_button_clicked_get_name), NULL);
    gtk_box_pack_start(GTK_BOX(box), button1, FALSE, FALSE, 0);

    // 创建按钮并关联数据
    GtkWidget *button2 = gtk_button_new_with_label("按钮2");
    g_object_set_data(G_OBJECT(button2), "button-id", (gpointer)"btn_002");  // 关联数据
    g_signal_connect(button2, "clicked", G_CALLBACK(on_button_clicked_get_data), NULL);
    gtk_box_pack_start(GTK_BOX(box), button2, FALSE, FALSE, 0);

    // 创建按钮使用标签作为标识符
    GtkWidget *button3 = gtk_button_new_with_label("F1 CQ");
    g_signal_connect(button3, "clicked", G_CALLBACK(on_button_clicked_use_label), NULL);
    gtk_box_pack_start(GTK_BOX(box), button3, FALSE, FALSE, 0);

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}